package com.lanyou.esb.redis.proxy.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.common.Constants4Redis;
import com.lanyou.esb.redis.JedisPoolUtils;
import com.lanyou.esb.redis.proxy.base.BaseHashJedisProxy;

/**
 * 服务可用性测试Redis操作代理
 * 
 * @author Davey.wu
 */
@Service
public class ServiceUsabilityJedisProxy extends BaseHashJedisProxy<String> {

	public ServiceUsabilityJedisProxy() {
		this.setHashKey(Constants4Redis.REDIS_HASH_KEY_IF_USAB_TEST);
	}

	@Override
	public String hget(String field) throws JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return "";
		}
		boolean broken = false;
		try {
			result = jedis.hget(this.getHashKey(), field);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	@Override
	public long hset(String field, String target)
			throws JsonProcessingException {
		Long result = 0l;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			result = jedis.hset(this.getHashKey(), field, target);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (result == null) {
			return 0;
		}
		return result.longValue();
	}

	@Override
	public Map<String, String> hgetAll(String key) {
		Map<String, String> result = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		try {
			result = jedis.hgetAll(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	@Override
	public long hdel(List<String> keys, String field) {
		if (keys == null || keys.size() == 0 || StringUtils.isBlank(field)) {
			return 0;
		}
		long result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			for (String key : keys) {
				Long temp = jedis.hdel(key, field);
				if (temp == null) {
					continue;
				}
				result += temp.longValue();
			}
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	/**
	 * 保存数据到REDIS
	 * 
	 * @param keys
	 *            :键值
	 * @param field
	 *            :域名
	 * @param target
	 *            ：保存目标
	 * @return
	 * @throws JsonProcessingException
	 */
	public static long hset(List<String[]> values)
			throws JsonProcessingException {
		if (values == null || values.isEmpty()) {
			return 0;
		}
		long result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			Pipeline pipe = jedis.pipelined();
			for (String[] value : values) {
				if (value.length != 3) {
					continue;
				}
				pipe.hset(value[0], value[1], value[2]);
			}
			pipe.sync();
			result = 1;
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	@Override
	public Map<String, String> getKeyMap(String target) {
		// TODO Auto-generated method stub
		return null;
	}

}
